#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tornado.web
import logging
import settings
import MySQLdb
import sys
sys.path.insert(0,'/home/liuyue/svn/wodfan/blog/')
sys.path.insert(0,'/opt/web/')
from Tools import getPid
import json
from _base import BaseHandler
import tornado.auth
import tornado.escape
import tornado.httpserver
import tornado.ioloop
import tornado.options
import hashlib
import os
import time

class HomeHandle(BaseHandler):
    
    @tornado.web.authenticated
    def get(self):
        start = self.get_argument('div3','')#起始时间
        end = self.get_argument('div4','')#结束时间
        pid = self.get_argument('pid','')#用户手动输入的pid
        ltype = self.get_argument('ltype','1')#用户所选择的客户端平台
        where = ""
        table = ""
        pids = ''
        if ltype == '1':
            ss = getPid.getPid_json('3')
            sc = json.read(str(ss))
            sh = []
            for value in sc['results']:
                bett = "'"+str(value)+"'"
                sh.append(bett)  
            pids = ','.join(sh)
            table = 'stat_day_total'
            where = ' where device_type = 2 and os_id = 61 and `business_type` = 2 and `partner_mode` = 3 and `pid_stat` = 1 and pid in (%s) ' % pids
        elif ltype == '2':
            ss = getPid.getPid_json('4')
            sc = json.read(str(ss))  
            sh = []
            for value in sc['results']:
                bett = "'"+str(value)+"'"
                sh.append(bett)  
            pids = ','.join(sh)
            table = 'stat_day_total'
            where = ' where device_type = 1 and os_id = 61 and `business_type` = 2 and `partner_mode` = 3 and `pid_stat` = 1 and pid in (%s) ' % pids
        elif ltype == '3':
            ss = getPid.getPid_json('1')
            sc = json.read(str(ss))  
            sh = []
            for value in sc['results']:
                bett = "'"+str(value)+"'"
                sh.append(bett)  
            pids = ','.join(sh)
            table = 'stat_day_total'
            where = ' where `os_id` = 52 and `device_type` = 2 and `business_type` = 2 and `partner_mode` = 3 and `pid_stat` = 1 and pid in (%s) ' % pids
        elif ltype == '4':
            ss = getPid.getPid_json('2')
            sc = json.read(str(ss))  
            sh = []
            for value in sc['results']:
                bett = "'"+str(value)+"'"
                sh.append(bett)  
            pids = ','.join(sh)
            table = 'stat_day_total'
            where = ' where `os_id` = 52 and `device_type` = 1 and `business_type` = 2 and `partner_mode` = 3 and `pid_stat` = 1 and pid in (%s) ' % pids
        elif ltype == '5':
            ss = getPid.getPid_json('5')
            sc = json.read(str(ss))  
            sh = []
            for value in sc['results']:
                bett = "'"+str(value)+"'"
                sh.append(bett)  
            pids = ','.join(sh)
            table = 'stat_day_total'
            where = ' where `os_id` = 59 and `device_type` = 1 and `business_type` = 2 and `partner_mode` = 3 and `pid_stat` = 1 and pid in (%s) ' % pids
        elif ltype == '6':
            ss = getPid.getPid_json('7')
            sc = json.read(str(ss))  
            sh = []
            for value in sc['results']:
                bett = "'"+str(value)+"'"
                sh.append(bett)  
            pids = ','.join(sh)
            table = 'stat_day_paike'
            where = ' where `os_id` = 61 and `device_type` = 1 and `business_type` = 4 and `partner_mode` = 3 and `pid_stat` = 1 and pid in (%s) ' % pids
        elif ltype == '7':
            ss = getPid.getPid_json('6')
            sc = json.read(str(ss))  
            sh = []
            for value in sc['results']:
                bett = "'"+str(value)+"'"
                sh.append(bett)  
            pids = ','.join(sh)
            table = 'stat_day_paike'
            where = ' where `os_id` = 52 and `device_type` = 1 and `business_type` = 4 and `partner_mode` = 3 and `pid_stat` = 1 and pid in (%s) ' % pids
        if start != '' and end != '':
            start = start.replace("-","")
            end = end.replace("-","")
            where = where + " and date between %s and %s" % (start,end)
        if pid != '':
            where = where + " and pid = '%s'" % pid
        mysql_config = settings.mysql_config      
        dbMysql = MySQLdb.connect(**mysql_config)
        c = dbMysql.cursor(MySQLdb.cursors.DictCursor)
        sql_factory = "SELECT count(id) as cs FROM  `%s` %s " % (table,where)
        c.execute(sql_factory)
        _count = c.fetchone()
        count=_count['cs']
        _page = self.get_argument('page', '1') #页码
        _pagenum = 15
        page = {}
        page['count'] = count
        page['total'] = count/_pagenum + 1 
        page['first'] = 1
        page['next'] = page['total'] if int(_page) + 1 > page['total'] else int(_page)+1
        page['previous'] = int(_page) -1 if int(_page)  > 1 else 1
        page['last'] = count/_pagenum + 1 
        sql_factory = "SELECT * FROM  `%s` %s " % (table,where)
        sql_factory = sql_factory + ' order by id desc '
        sql_factory = sql_factory + ' limit {0}, {1}'.format((int(_page)-1)*int(_pagenum), _pagenum)
        print sql_factory
        c.execute(sql_factory)
        entries = c.fetchall()
        nav = 'home'
        self.render("index.html",entries=entries,nav=nav,page=page,div3=start,div4=end,pid=pid,ltype=ltype,_page=_page)
        
class Ulist(BaseHandler):
    
    @tornado.web.authenticated
    def get(self):      
        mysql_config = settings.mysql_config      
        dbMysql = MySQLdb.connect(**mysql_config)
        c = dbMysql.cursor(MySQLdb.cursors.DictCursor)
        sql_factory = "SELECT count(id) as cs FROM  `stat_user` "
        c.execute(sql_factory)
        _count = c.fetchone()
        count=_count['cs']
        _page = self.get_argument('page', '1') #页码
        _pagenum = 15
        page = {}
        page['count'] = count
        page['total'] = count/_pagenum + 1 
        page['first'] = 1
        page['next'] = page['total'] if int(_page) + 1 > page['total'] else int(_page)+1
        page['previous'] = int(_page) -1 if int(_page)  > 1 else 1
        page['last'] = count/_pagenum + 1 
        sql_factory = "SELECT * FROM  `stat_user` "
        sql_factory = sql_factory + ' order by id desc '
        sql_factory = sql_factory + ' limit {0}, {1}'.format((int(_page)-1)*int(_pagenum), _pagenum)
        c.execute(sql_factory)
        entries = c.fetchall()
        nav = 'ulist'
        self.render("ulist.html",entries=entries,page=page,nav=nav)
        
class LogList(BaseHandler):
    
    @tornado.web.authenticated
    def get(self):      
        mysql_config = settings.mysql_config      
        dbMysql = MySQLdb.connect(**mysql_config)
        c = dbMysql.cursor(MySQLdb.cursors.DictCursor)
        username = self.get_secure_cookie("user")
        sql_factory = "SELECT * FROM  `stat_user` where username = '%s' " % username
        c.execute(sql_factory)
        user_id = c.fetchone()
        sql_factory = "SELECT count(id) as cs FROM  `stat_log` where user_id = '%s' " % user_id['id']
        c.execute(sql_factory)
        _count = c.fetchone()
        count=_count['cs']
        _page = self.get_argument('page', '1') #页码
        _pagenum = 15
        page = {}
        page['count'] = count
        page['total'] = count/_pagenum + 1 
        page['first'] = 1
        page['next'] = page['total'] if int(_page) + 1 > page['total'] else int(_page)+1
        page['previous'] = int(_page) -1 if int(_page)  > 1 else 1
        page['last'] = count/_pagenum + 1 
        sql_factory = "SELECT * FROM  `stat_log` where user_id = '%s' " % user_id['id']
        sql_factory = sql_factory + ' order by id desc '
        sql_factory = sql_factory + ' limit {0}, {1}'.format((int(_page)-1)*int(_pagenum), _pagenum)
        c.execute(sql_factory)
        entries = c.fetchall()
        nav = 'loglist'
        self.render("log.html",entries=entries,page=page,nav=nav)
        
        
class ActionList(BaseHandler):
    
    @tornado.web.authenticated
    def get(self):
        datetime = self.get_argument('date',time.strftime('%Y%m%d',time.localtime(time.time()))) #日期
        datetime = datetime.replace("-","")
        mysql_config = settings.mysql_config      
        dbMysql = MySQLdb.connect(**mysql_config)
        c = dbMysql.cursor(MySQLdb.cursors.DictCursor)
        sql_factory = "SELECT count(id) as cs FROM  `stat_action` where date = '%s' " % datetime
        c.execute(sql_factory)
        _count = c.fetchone()
        count=_count['cs']
        _page = self.get_argument('page','1') #页码
        _pagenum = 15
        page = {}
        page['count'] = count
        page['total'] = count/_pagenum + 1 
        page['first'] = 1
        page['next'] = page['total'] if int(_page) + 1 > page['total'] else int(_page)+1
        page['previous'] = int(_page) -1 if int(_page)  > 1 else 1
        page['last'] = count/_pagenum + 1 
        sql_factory = "SELECT * FROM  `stat_action` where date = '%s' " % datetime
        sql_factory = sql_factory + ' order by id desc '
        sql_factory = sql_factory + ' limit {0}, {1}'.format((int(_page)-1)*int(_pagenum), _pagenum)
        c.execute(sql_factory)
        entries = c.fetchall()
        nav = 'actionlist'
        self.render("actionlist.html",entries=entries,page=page,nav=nav,date=datetime)
        


class Logout(BaseHandler):
    
    def get(self):
        username = self.get_secure_cookie("user")
        datetime = time.strftime('%Y%m%d',time.localtime(time.time()))
        mysql_config = settings.mysql_config      
        dbMysql = MySQLdb.connect(**mysql_config)
        c = dbMysql.cursor(MySQLdb.cursors.DictCursor)
        c.execute("select * from stat_user where username = '%s'" % username )
        one = c.fetchone()
        c.execute(" insert into `stat_action` (`user_name`,`user_id`,`user_action`,`date`) values ('%s','%s','%s','%s')" % (username,one['id'],'logout',datetime))
        c.execute("commit")
        self.clear_cookie("user")
        self.redirect("/log")

class Test(BaseHandler):
    
    def get(self):
        """以下代码都是测试用的ss = getPid.getPid_json('2')
        sc = json.read(str(ss))
        sh = []
        for value in sc['results']:
            bett = "'"+str(value)+"'"
            sh.append(bett)  
        print ','.join(sh)
        """
        
        username = self.get_secure_cookie("user")
        datetime = time.strftime('%Y%m%d',time.localtime(time.time()))
        mysql_config = settings.mysql_config      
        dbMysql = MySQLdb.connect(**mysql_config)
        c = dbMysql.cursor(MySQLdb.cursors.DictCursor)
        c.execute("select * from stat_user where username = '%s'" % username )
        one = c.fetchone()
        c.execute(" insert into `stat_action` (`user_name`,`user_id`,`user_action`,`date`) values ('%s','%s','%s','%s')" % (username,one['id'],'logout',datetime))
        c.execute("commit")
        
        #c.execute("SET NAMES utf8")
        #c.execute("SET CHARACTER_SET_CLIENT=utf8")
        #c.execute("SET CHARACTER_SET_RESULTS=utf8")
        
        #user_name = user_name.decode('utf-8')
        #sql_factory = " insert into stat_action (user_id,user_name,user_action,date) values ('%s','%s','%s','%s') " % (1,user_name,'123',0)
        #c.execute(sql_factory)
        #c.execute("commit")
        
class AddLog(BaseHandler):
    
    @tornado.web.authenticated
    def get(self):
        username = self.get_secure_cookie("user")
        nav = 'loglist'
        mysql_config = settings.mysql_config      
        dbMysql = MySQLdb.connect(**mysql_config)
        c = dbMysql.cursor(MySQLdb.cursors.DictCursor)
        sql_factory = "SELECT * FROM  `stat_user` where username = '%s'" % username
        c.execute(sql_factory)
        entrie = c.fetchone()
        userid = entrie['id']
        self.render("addlog.html",nav=nav,userid=userid)
        
        
class EditLog(BaseHandler):
    
    @tornado.web.authenticated
    def get(self):
        logid = self.get_argument('logid')
        nav = 'loglist'
        mysql_config = settings.mysql_config      
        dbMysql = MySQLdb.connect(**mysql_config)
        c = dbMysql.cursor(MySQLdb.cursors.DictCursor)
        sql_factory = "SELECT * FROM  `stat_log` where id = '%s'" % logid
        c.execute(sql_factory)
        entrie = c.fetchone()
        self.render("editlog.html",nav=nav,entrie=entrie)

class Login(BaseHandler):
  
    def post(self):
        username = self.get_argument('username')
        password = self.get_argument('passwd')
        message = 'ok'
        mysql_config = settings.mysql_config      
        dbMysql = MySQLdb.connect(**mysql_config)
        c = dbMysql.cursor()
        sql_factory = "SELECT * FROM  `stat_user` where username = '%s'" % username
        datetime = time.strftime('%Y%m%d',time.localtime(time.time()))
        c.execute(sql_factory)
        entrie = c.fetchone()
        if (entrie == None) or (username != password):
            message = 'failed'
        else:
            self.set_secure_cookie("user",str(username))
            c.execute(" insert into `stat_action` (`user_name`,`user_id`,`user_action`,`date`) values ('%s','%s','%s','%s')" % (username,entrie[0],'login',datetime))
            c.execute("commit")
        self.write(message)
        
class Pixiu(BaseHandler):
  
    def post(self):
        pids = str(self.get_argument('pid'))
        ltype = str(self.get_argument('ltype'))
        pids = pids.split(",")
        mysql_config = settings.mysql_config      
        dbMysql = MySQLdb.connect(**mysql_config)
        c = dbMysql.cursor()
        table = ''
        if ltype == '1':
            
            table = 'stat_day_total'
            
        elif ltype == '2':
            
            table = 'stat_day_total'
            
        elif ltype == '3':
            
            table = 'stat_day_total'
            
        elif ltype == '4':
            
            table = 'stat_day_total'
            
        elif ltype == '5':
            
            table = 'stat_day_total'
            
        elif ltype == '6':
            
            table = 'stat_day_paike'
            
        elif ltype == '7':
       
            table = 'stat_day_paike'
        
        for value in pids:
            value = str(value)
            _value = value.split("+")
            sql_factory = "update `%s` set pv = 0,uv = 0, vv =0 ,newuser = 0,register = 0  where pid = '%s' and date = '%s' " % (table,_value[0],_value[1])
            c.execute(sql_factory)
            c.execute("commit")
            
            username = self.get_secure_cookie("user")
            datetime = time.strftime('%Y%m%d',time.localtime(time.time()))
            mysql_config = settings.mysql_config      
            dbMysql = MySQLdb.connect(**mysql_config)
            c = dbMysql.cursor(MySQLdb.cursors.DictCursor)
            c.execute("select * from stat_user where username = '%s'" % username )
            one = c.fetchone()
            d = {'"':'\\"', "'":"\\'", "\0":"\\\0", "\\":"\\\\"}
            sql_factory = ''.join(d.get(c, c) for c in sql_factory)
            c.execute(" insert into `stat_action` (`user_name`,`user_id`,`user_action`,`date`) values ('%s','%s','%s','%s')" % (username,one['id'],sql_factory,datetime))
            c.execute("commit")
              
        self.write("ok")
        
class Setdanz(BaseHandler):
  
    def post(self):
        datetime = self.get_argument('date')
        pid = self.get_argument('pid')
        pv = self.get_argument('pv')
        uv = self.get_argument('uv')
        vv = self.get_argument('vv')
        tourl = self.get_argument('tourl')
        newuser = self.get_argument('newuser')
        register = self.get_argument('register')
        ltype = self.get_argument('ltype','1')
        table = ''
        
        if ltype == '1':
            
            table = 'stat_day_total'
            
        elif ltype == '2':
            
            table = 'stat_day_total'
            
        elif ltype == '3':
            
            table = 'stat_day_total'
            
        elif ltype == '4':
            
            table = 'stat_day_total'
            
        elif ltype == '5':
            
            table = 'stat_day_total'
            
        elif ltype == '6':
            
            table = 'stat_day_paike'
            
        elif ltype == '7':
       
            table = 'stat_day_paike'
            
        
        mysql_config = settings.mysql_config      
        dbMysql = MySQLdb.connect(**mysql_config)
        c = dbMysql.cursor()
        sql_factory = "update `%s` set pv= '%s',uv='%s',vv='%s',newuser='%s',register='%s' where pid = '%s' and date = '%s'" % (table,pv,uv,vv,newuser,register,pid,datetime)
        c.execute(sql_factory)
        c.execute("commit")
        
        username = self.get_secure_cookie("user")
        datetime = time.strftime('%Y%m%d',time.localtime(time.time()))
        mysql_config = settings.mysql_config      
        dbMysql = MySQLdb.connect(**mysql_config)
        c = dbMysql.cursor(MySQLdb.cursors.DictCursor)
        c.execute("select * from stat_user where username = '%s'" % username )
        one = c.fetchone()
        d = {'"':'\\"', "'":"\\'", "\0":"\\\0", "\\":"\\\\"}
        sql_factory = ''.join(d.get(c, c) for c in sql_factory)
        c.execute(" insert into `stat_action` (`user_name`,`user_id`,`user_action`,`date`) values ('%s','%s','%s','%s')" % (username,one['id'],sql_factory,datetime))
        c.execute("commit")
        
        self.redirect(str(tourl)+"")

class Log(BaseHandler):
    
    def get(self):
        username = self.get_secure_cookie("user")
        if(username):self.redirect("/")
        self.render("login.html")
        
        
class InsertUser(BaseHandler):    
    @tornado.web.authenticated
    def post(self):         
        mysql_config = settings.mysql_config      
        dbMysql = MySQLdb.connect(**mysql_config)
        c = dbMysql.cursor()
        username = self.get_argument('username')
        sql_factory = "SELECT * FROM  `stat_user` where username = '%s'" % username
        c.execute(sql_factory)
        entrie = c.fetchone()    
        message = 'ok'
        if entrie != None:
            message = 'failed'
        else:
            try:
                m = hashlib.md5(username)
                m.digest()
                password = m.hexdigest()
            except:
                password = 'unknow'#不允许中文帐号的存在
            c.execute("insert into  `stat_user` (`username`,`password`,`fid`,`type`,`realname`) values ('%s','%s','%s','%s','%s')" % (username,password,0,1,'') )
            c.execute("commit")
            
            sql_factory = "insert into  `stat_user` (`username`,`password`,`fid`,`type`,`realname`) values ('%s','%s','%s','%s','%s')" % (username,password,0,1,'')
            username = self.get_secure_cookie("user")
            datetime = time.strftime('%Y%m%d',time.localtime(time.time()))
            mysql_config = settings.mysql_config      
            dbMysql = MySQLdb.connect(**mysql_config)
            c = dbMysql.cursor(MySQLdb.cursors.DictCursor)
            c.execute("select * from stat_user where username = '%s'" % username )
            one = c.fetchone()
            d = {'"':'\\"', "'":"\\'", "\0":"\\\0", "\\":"\\\\"}
            sql_factory = ''.join(d.get(c, c) for c in sql_factory)
            c.execute(" insert into `stat_action` (`user_name`,`user_id`,`user_action`,`date`) values ('%s','%s','%s','%s')" % (username,one['id'],sql_factory,datetime))
            c.execute("commit")
        
        self.write(message)


class UpdateUser(BaseHandler):    
    @tornado.web.authenticated
    def post(self):         
        mysql_config = settings.mysql_config      
        dbMysql = MySQLdb.connect(**mysql_config)
        c = dbMysql.cursor()
        username = self.get_argument('username')
        userid = self.get_argument('userid')
        sql_factory = "SELECT * FROM  `stat_user` where username = '%s' " % username
        c.execute(sql_factory)
        entrie = c.fetchone()    
        message = 'ok'
        if entrie != None:
            message = 'failed'
        else:
            c.execute("update `stat_user` set username = '%s' where id = '%s'" % (username,userid) )
            c.execute("commit")
            sql_factory = "update `stat_user` set username = '%s' where id = '%s'" % (username,userid)
            username = self.get_secure_cookie("user")
            datetime = time.strftime('%Y%m%d',time.localtime(time.time()))
            mysql_config = settings.mysql_config      
            dbMysql = MySQLdb.connect(**mysql_config)
            c = dbMysql.cursor(MySQLdb.cursors.DictCursor)
            c.execute("select * from stat_user where username = '%s'" % username )
            one = c.fetchone()
            d = {'"':'\\"', "'":"\\'", "\0":"\\\0", "\\":"\\\\"}
            sql_factory = ''.join(d.get(c, c) for c in sql_factory)
            c.execute(" insert into `stat_action` (`user_name`,`user_id`,`user_action`,`date`) values ('%s','%s','%s','%s')" % (username,one['id'],sql_factory,datetime))
            c.execute("commit")
        self.write(message)
        
class InsertLog(BaseHandler):    
    @tornado.web.authenticated
    
    def post(self):         
        mysql_config = settings.mysql_config
        d = {'"':'\\"', "'":"\\'", "\0":"\\\0", "\\":"\\\\"}      
        dbMysql = MySQLdb.connect(**mysql_config)
        c = dbMysql.cursor()
        title = self.get_argument('title')
        content = self.get_argument('content')
        content = ''.join(d.get(c, c) for c in content)
        title = ''.join(d.get(c, c) for c in title)
        userid = self.get_argument('userid')
        datetime = time.strftime('%Y%m%d',time.localtime(time.time()))
        sql_factory = "SELECT * FROM  `stat_log` where title = '%s'" % title
        c.execute(sql_factory)
        entrie = c.fetchone()    
        message = 'ok'
        if entrie != None:
            message = 'failed'
        else:
            c.execute("insert into `stat_log` (`title`,`content`,`user_id`,`date`) values ('%s','%s','%s','%s')" % (title,content,userid,datetime) )
            c.execute("commit")
            

            sql_factory = "insert into `stat_log` (`title`,`content`,`user_id`,`date`) values ('%s','%s','%s','%s')" % (title,content,userid,datetime)
            username = self.get_secure_cookie("user")
            datetime = time.strftime('%Y%m%d',time.localtime(time.time()))
            mysql_config = settings.mysql_config      
            dbMysql = MySQLdb.connect(**mysql_config)
            c = dbMysql.cursor(MySQLdb.cursors.DictCursor)
            c.execute("select * from stat_user where username = '%s'" % username )
            one = c.fetchone()
            d = {'"':'\\"', "'":"\\'", "\0":"\\\0", "\\":"\\\\"}
            sql_factory = ''.join(d.get(c, c) for c in sql_factory)
            c.execute(" insert into `stat_action` (`user_name`,`user_id`,`user_action`,`date`) values ('%s','%s','%s','%s')" % (username,one['id'],sql_factory,datetime))
            c.execute("commit")
        self.write(message)
        

class UpdateLog(BaseHandler):    
    @tornado.web.authenticated
    
    def post(self):         
        mysql_config = settings.mysql_config
        d = {'"':'\\"', "'":"\\'", "\0":"\\\0", "\\":"\\\\"}      
        dbMysql = MySQLdb.connect(**mysql_config)
        c = dbMysql.cursor()
        title = self.get_argument('title')
        content = self.get_argument('content')
        content = ''.join(d.get(c, c) for c in content)
        title = ''.join(d.get(c, c) for c in title)
        logid = self.get_argument('logid')
        sql_factory = "SELECT * FROM  `stat_log` where title = '%s'" % title
        c.execute(sql_factory)
        entrie = c.fetchone()    
        message = 'ok'
        
        c.execute("update `stat_log` set `title` = '%s',`content` = '%s' where id = '%s' " % (title,content,logid) )
        c.execute("commit")
        
        sql_factory = "update `stat_log` set `title` = '%s',`content` = '%s' where id = '%s' " % (title,content,logid)
        username = self.get_secure_cookie("user")
        datetime = time.strftime('%Y%m%d',time.localtime(time.time()))
        mysql_config = settings.mysql_config      
        dbMysql = MySQLdb.connect(**mysql_config)
        c = dbMysql.cursor(MySQLdb.cursors.DictCursor)
        c.execute("select * from stat_user where username = '%s'" % username )
        one = c.fetchone()
        d = {'"':'\\"', "'":"\\'", "\0":"\\\0", "\\":"\\\\"}
        sql_factory = ''.join(d.get(c, c) for c in sql_factory)
        c.execute(" insert into `stat_action` (`user_name`,`user_id`,`user_action`,`date`) values ('%s','%s','%s','%s')" % (username,one['id'],sql_factory,datetime))
        c.execute("commit")
        
        self.write(message)
        
class DeleteLog(BaseHandler):    
    @tornado.web.authenticated
    
    def post(self):         
        mysql_config = settings.mysql_config   
        dbMysql = MySQLdb.connect(**mysql_config)
        c = dbMysql.cursor()
        logid = self.get_argument('logid')
        c.execute("delete from `stat_log`  where id = '%s' " % logid )
        c.execute("commit")
        
        sql_factory = "delete from `stat_log`  where id = '%s' " % logid
        username = self.get_secure_cookie("user")
        datetime = time.strftime('%Y%m%d',time.localtime(time.time()))
        mysql_config = settings.mysql_config      
        dbMysql = MySQLdb.connect(**mysql_config)
        c = dbMysql.cursor(MySQLdb.cursors.DictCursor)
        c.execute("select * from stat_user where username = '%s'" % username )
        one = c.fetchone()
        d = {'"':'\\"', "'":"\\'", "\0":"\\\0", "\\":"\\\\"}
        sql_factory = ''.join(d.get(c, c) for c in sql_factory)
        c.execute(" insert into `stat_action` (`user_name`,`user_id`,`user_action`,`date`) values ('%s','%s','%s','%s')" % (username,one['id'],sql_factory,datetime))
        c.execute("commit")
        
        self.write('ok')

class DeleteUser(BaseHandler):    
    @tornado.web.authenticated
    def post(self):         
        mysql_config = settings.mysql_config      
        dbMysql = MySQLdb.connect(**mysql_config)
        c = dbMysql.cursor()
        userid = self.get_argument('userid')
        sql_factory = "delete from `stat_user` where id = '%s' " % userid
        c.execute(sql_factory)
        c.execute("commit")
        
        username = self.get_secure_cookie("user")
        datetime = time.strftime('%Y%m%d',time.localtime(time.time()))
        mysql_config = settings.mysql_config      
        dbMysql = MySQLdb.connect(**mysql_config)
        c = dbMysql.cursor(MySQLdb.cursors.DictCursor)
        c.execute("select * from stat_user where username = '%s'" % username )
        one = c.fetchone()
        d = {'"':'\\"', "'":"\\'", "\0":"\\\0", "\\":"\\\\"}
        sql_factory = ''.join(d.get(c, c) for c in sql_factory)
        c.execute(" insert into `stat_action` (`user_name`,`user_id`,`user_action`,`date`) values ('%s','%s','%s','%s')" % (username,one['id'],sql_factory,datetime))
        c.execute("commit")
        
        
        self.write('ok')    
        
            


        
        
        




